package com.nanhai.buddhist.college.module.study.util;

import cn.hutool.core.util.ObjectUtil;
import com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil;
import com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author AKai
 * @date 2022/8/26
 */
public class DateConvertUtil {

    /**
     * 根据日期  获取开始时间
     *
     * @param date
     * @return
     */
    public static Date getBeginOfDay(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        return now.getTime();
    }

    /**
     * 根据日期  获取结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndOfDay(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(getBeginOfDay(date));
        now.add(Calendar.DAY_OF_YEAR, 1);
        now.add(Calendar.MILLISECOND, -1);
        return now.getTime();
    }

    /**
     * 设置时间 时分秒
     *
     * @param timeStr
     * @param date
     * @return
     */
    public static Date setTimeHMS(String timeStr, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String[] split = timeStr.split(":");
        // 时
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0].replace(" ", "")));
        // 分
        calendar.set(Calendar.MINUTE, Integer.parseInt(split[1].replace(" ", "")));
        // 秒
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * dateStr  yyyy-MM-dd
     *
     * @param dateStr
     * @return
     */
    public static Date stringToDateEnd(String dateStr) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse;
        try {
            parse = sdf.parse(dateStr);
        } catch (Exception e) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.DATE_FORM_EXISTS);
        }
        return getEndOfDay(parse);
    }

    /**
     * 开始时间
     *
     * @param dateStr
     * @return
     */
    public static Date stringToDateStart(String dateStr) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse;
        try {
            parse = sdf.parse(dateStr);
        } catch (Exception e) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.DATE_FORM_EXISTS);
        }
        return getBeginOfDay(parse);
    }


   /* *//**
     * JAVA计算两个日期相差多少天(by Date String with format "yyyy-MM-dd")
     *
     * @author zhengkai.blog.csdn.net
     *//*
    public static int daysBetween(Date startDate, Integer number) {
        if (ObjectUtil.isNull(startDate) || ObjectUtil.isNull(number)) {
            return 0;
        }
        Date beginOfDay = getBeginOfDay(startDate);
        Date endOfDay = getEndOfDay(endDate);
        return  (int) ((endOfDay.getTime() - beginOfDay.getTime()) / (1000 * 3600 * 24));
    }*/

    /**
     *  startDate  + number 天
     *  时分秒 ： 00：00:00
     * @param startDate
     * @param number
     * @return
     */
    public static Date weekStartDate(Date startDate, Integer number) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_YEAR, number);
        return getBeginOfDay(startDate);
    }
    /**
     *  startDate  + number 天
     *  时分秒 ： 23：59:59
     * @param endDate
     * @param number
     * @return
     */
    public static Date weekEndDate(Date endDate, Integer number) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_YEAR, number);
        return getEndOfDay(endDate);
    }
}
